<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改文件管理')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: jasny-bootstrap-css"/>
    <th:block th:include="include :: jasny-bootstrap-js"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-file-edit" th:object="${fileManagement}">
            <input name="fileId" th:field="*{fileId}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件名：</label>
                <div class="col-sm-8">
                    <input name="fileName" th:field="*{fileName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件类型：</label>
                <div class="col-sm-8">
                    <input name="fileType" th:field="*{fileType}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件大小：</label>
                <div class="col-sm-8">
                    <input name="fileSize" th:field="*{fileSize}" class="form-control" type="text">
                </div>
            </div>
            <!---->
            <!---->
            <!---->
            <!--<div class="form-group">
                <label class="col-sm-3 control-label">文件体：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="fileBody" th:field="*{fileBody}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="fileBody" name="file" type="file">
                    </div>
                </div>
            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">技师视频：</label>
                <div class="col-sm-8">
                    <input id="technician_video" name="technicianVideo" th:field="*{fileBody}" class="form-control" type="text" style="display:none ">
                    <div class="fileinput fileinput-new" data-provides="fileinput" id="inputTechnicianVideo">
                        <!-- 此div用来预览需要修改的视频 -->
                        <div class="fileinput-new thumbnail" data-trigger="fileinput">
                            <video controls="controls" width="100%">
                                <source type="video/mp4" th:src="*{fileBody}"/>
                            </video>
                        </div>
                        <!-- 此div用来回显预览上传完成后的视频 -->
                        <div class="fileinput-preview fileinput-exists thumbnail" id="viderSource">
                        </div>
                        <div>
                        <span class="btn btn-white btn-file">
                            <span class="fileinput-new">选择文件</span>
                            <span class="fileinput-exists">更改</span>
                            <input type="file" id="technicianVideo">
                        </span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
            </div>

            <!---->
            <!---->
            <!---->
            <div class="form-group">
                <label class="col-sm-3 control-label">文件地址：</label>
                <div class="col-sm-8">
                    <input name="fileAddress" th:field="*{fileAddress}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">乐观锁：</label>
                <div class="col-sm-8">
                    <input name="revision" th:field="*{revision}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "course/file";
        $("#form-file-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-file-edit').serialize());
            }
        }

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });


        /*上传开始*/
        $('#inputTechnicianVideo').on('change.bs.fileinput ', function (e) {
            let imgFile = $('#technicianVideo')[0].files[0];
            let video = "video";
            uploadFile(imgFile, video);
        });
        //ruoyi通用的上传接口，不需要自己写后台，框架自带
        function uploadFile(imgFile, imgId) {
            var formData = new FormData();
            if (imgFile == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            formData.append('file', imgFile);
            $.ajax({
                url: "/common/upload",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                async: false,
                success: function (result) {
                    //给input赋值
                    $('#technician_video').val(result.fileName);
                    //用来回显预览视频
                    let videoElement = '<video controls="controls" width="100%"><source type="video/mp4" src="' + result.fileName + '"/></video>\n';
                    $("#viderSource").append(videoElement);
                }
            });
        }
        /*上传结束*/
    </script>
</body>
</html>